package com.campus.marketplace.config;

import com.campus.marketplace.entity.User;
import com.campus.marketplace.entity.Product;
import com.campus.marketplace.entity.Comment;
import com.campus.marketplace.entity.Report;
import com.campus.marketplace.repository.UserRepository;
import com.campus.marketplace.repository.ProductRepository;
import com.campus.marketplace.repository.CommentRepository;
import com.campus.marketplace.repository.ReportRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;

/**
 * 测试数据初始化类
 * 在应用程序启动时自动执行，用于创建测试数据
 */
@Component
public class TestDataInitializer implements CommandLineRunner {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private ProductRepository productRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private ReportRepository reportRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public void run(String... args) {
        System.out.println("\n=== 开始初始化测试数据 ===");

        // 检查是否已经存在数据
        long userCount = userRepository.count();
        System.out.println("当前用户数量: " + userCount);

        if (userCount > 0) {
            System.out.println("数据库中已存在数据，跳过初始化...");
            System.out.println("=== 数据初始化结束 ===\n");
            return;
        }

        System.out.println("开始创建测试数据...");

        // 创建测试用户数据
        System.out.println("\n1. 创建用户数据...");
        // 1. 创建普通用户1
        User user1 = new User();
        user1.setUsername("zhangsan"); // 用户名
        user1.setPassword(passwordEncoder.encode("123456")); // 密码
        user1.setEmail("zhangsan@example.com"); // 邮箱
        user1.setPhone("13800138001"); // 手机号
        user1.setRole("USER"); // 用户角色
        System.out.println("创建用户1: " + user1.getUsername() + ", 邮箱: " + user1.getEmail() + ", 角色: " + user1.getRole());

        // 2. 创建普通用户2
        User user2 = new User();
        user2.setUsername("lisi");
        user2.setPassword(passwordEncoder.encode("123456"));
        user2.setEmail("lisi@example.com");
        user2.setPhone("13800138002");
        user2.setRole("USER");
        System.out.println("创建用户2: " + user2.getUsername() + ", 邮箱: " + user2.getEmail() + ", 角色: " + user2.getRole());

        // 3. 创建普通用户3
        User user3 = new User();
        user3.setUsername("wangwu");
        user3.setPassword(passwordEncoder.encode("123456"));
        user3.setEmail("wangwu@example.com");
        user3.setPhone("13800138003");
        user3.setRole("USER");
        System.out.println("创建用户3: " + user3.getUsername() + ", 邮箱: " + user3.getEmail() + ", 角色: " + user3.getRole());

        // 4. 创建管理员用户
        User admin = new User();
        admin.setUsername("admin");
        admin.setPassword(passwordEncoder.encode("admin123"));
        admin.setEmail("admin@example.com");
        admin.setPhone("13800138000");
        admin.setRole("ADMIN");
        System.out.println("创建管理员: " + admin.getUsername() + ", 邮箱: " + admin.getEmail() + ", 角色: " + admin.getRole());

        // 保存用户数据
        userRepository.saveAll(Arrays.asList(user1, user2, user3, admin));
        System.out.println("用户数据创建完成，当前用户数量: " + userRepository.count());

        // 创建测试商品数据
        System.out.println("\n2. 创建商品数据...");
        // 1. 创建二手笔记本电脑
        Product product1 = new Product();
        product1.setTitle("联想ThinkPad X1 Carbon 2023款");
        product1.setDescription("9成新，使用一年，配置：i7-1165G7/16GB/512GB SSD/14寸2K屏，无维修，发票齐全");
        product1.setPrice(new BigDecimal("6999.00"));
        product1.setSeller(user1);
        product1.setStatus("ON_SALE");
        System.out.println("创建商品1: " + product1.getTitle() + ", 价格: " + product1.getPrice() + ", 卖家: "
                + product1.getSeller().getUsername());

        // 2. 创建二手手机
        Product product2 = new Product();
        product2.setTitle("iPhone 13 Pro Max 256GB");
        product2.setDescription("8成新，使用半年，无维修，电池健康度95%，配件齐全");
        product2.setPrice(new BigDecimal("5999.00"));
        product2.setSeller(user2);
        product2.setStatus("ON_SALE");
        System.out.println("创建商品2: " + product2.getTitle() + ", 价格: " + product2.getPrice() + ", 卖家: "
                + product2.getSeller().getUsername());

        // 3. 创建二手相机
        Product product3 = new Product();
        product3.setTitle("索尼A7M3 全画幅微单相机");
        product3.setDescription("95新，使用一年，快门次数5000次，带28-70mm镜头，箱说齐全");
        product3.setPrice(new BigDecimal("12000.00"));
        product3.setSeller(user1);
        product3.setStatus("ON_SALE");
        System.out.println("创建商品3: " + product3.getTitle() + ", 价格: " + product3.getPrice() + ", 卖家: "
                + product3.getSeller().getUsername());

        // 保存商品数据
        productRepository.saveAll(Arrays.asList(product1, product2, product3));
        System.out.println("商品数据创建完成，当前商品数量: " + productRepository.count());

        // 创建测试评论数据 - 展示三级评论嵌套
        System.out.println("\n3. 创建评论数据...");
        System.out.println("----------------------------------------");
        System.out.println("评论系统测试数据:");
        System.out.println("1. 三级评论嵌套结构");
        System.out.println("2. @用户交互功能");
        System.out.println("3. 评论状态管理");
        System.out.println("4. 敏感词检测");
        System.out.println("----------------------------------------");

        // 1. 一级评论
        Comment comment1 = new Comment();
        comment1.setProduct(product1);
        comment1.setUser(user2);
        comment1.setContent("@zhangsan 这个笔记本配置不错，价格也合理，请问可以面交吗？");
        comment1.setLevel(1);
        comment1.setStatus("APPROVED");
        System.out.println("\n创建一级评论:");
        System.out.println("用户: " + comment1.getUser().getUsername());
        System.out.println("商品: " + comment1.getProduct().getTitle());
        System.out.println("内容: " + comment1.getContent());
        System.out.println("状态: " + comment1.getStatus());

        // 2. 二级评论（回复一级评论）
        Comment comment2 = new Comment();
        comment2.setProduct(product1);
        comment2.setUser(user1);
        comment2.setParent(comment1);
        comment2.setContent("@lisi 可以的，我在学校图书馆附近，方便的话可以约时间看货");
        comment2.setLevel(2);
        comment2.setStatus("APPROVED");
        System.out.println("\n创建二级评论:");
        System.out.println("用户: " + comment2.getUser().getUsername());
        System.out.println("回复: " + comment2.getParent().getUser().getUsername());
        System.out.println("内容: " + comment2.getContent());
        System.out.println("状态: " + comment2.getStatus());

        // 3. 三级评论（回复二级评论）
        Comment comment3 = new Comment();
        comment3.setProduct(product1);
        comment3.setUser(user2);
        comment3.setParent(comment2);
        comment3.setContent("@zhangsan 好的，我明天下午有空，可以约在图书馆门口见面吗？");
        comment3.setLevel(3);
        comment3.setStatus("APPROVED");
        System.out.println("\n创建三级评论:");
        System.out.println("用户: " + comment3.getUser().getUsername());
        System.out.println("回复: " + comment3.getParent().getUser().getUsername());
        System.out.println("内容: " + comment3.getContent());
        System.out.println("状态: " + comment3.getStatus());

        // 4. 待审核的评论（包含敏感词）
        Comment comment4 = new Comment();
        comment4.setProduct(product2);
        comment4.setUser(user1);
        comment4.setContent("这个手机看起来不错，能便宜点吗？5000可以吗？");
        comment4.setLevel(1);
        comment4.setStatus("PENDING");
        System.out.println("\n创建待审核评论:");
        System.out.println("用户: " + comment4.getUser().getUsername());
        System.out.println("商品: " + comment4.getProduct().getTitle());
        System.out.println("内容: " + comment4.getContent());
        System.out.println("状态: " + comment4.getStatus());
        System.out.println("注意: 该评论需要审核");

        // 5. 被拒绝的评论（包含敏感词）
        Comment comment5 = new Comment();
        comment5.setProduct(product2);
        comment5.setUser(user3);
        comment5.setContent("这个手机是假的，大家不要买！");
        comment5.setLevel(1);
        comment5.setStatus("REJECTED");
        System.out.println("\n创建被拒绝评论:");
        System.out.println("用户: " + comment5.getUser().getUsername());
        System.out.println("商品: " + comment5.getProduct().getTitle());
        System.out.println("内容: " + comment5.getContent());
        System.out.println("状态: " + comment5.getStatus());
        System.out.println("原因: 包含敏感词和不当言论");

        // 保存评论数据
        commentRepository.saveAll(Arrays.asList(comment1, comment2, comment3, comment4, comment5));
        System.out.println("\n评论数据创建完成，当前评论数量: " + commentRepository.count());

        // 创建测试举报数据 - 展示多种举报类型和处理状态
        System.out.println("\n4. 创建举报数据...");
        System.out.println("----------------------------------------");
        System.out.println("举报系统测试数据:");
        System.out.println("1. 多类型举报表单");
        System.out.println("2. 举报状态追踪");
        System.out.println("3. 附件上传功能");
        System.out.println("4. 跨模块数据关联");
        System.out.println("----------------------------------------");

        // 1. 虚假信息举报（待处理）
        Report report1 = new Report();
        report1.setReporter(user1);
        report1.setTargetType("PRODUCT");
        report1.setTargetId(product2.getId());
        report1.setReportType("FAKE_INFO");
        report1.setContent("商品描述与实际不符，手机有明显划痕，电池健康度只有85%");
        report1.setEvidenceUrl("http://example.com/evidence1.jpg");
        report1.setStatus("PENDING");
        System.out.println("\n创建虚假信息举报:");
        System.out.println("举报人: " + report1.getReporter().getUsername());
        System.out.println("目标类型: " + report1.getTargetType());
        System.out.println("目标ID: " + report1.getTargetId());
        System.out.println("举报类型: " + report1.getReportType());
        System.out.println("举报内容: " + report1.getContent());

        System.out.println("状态: " + report1.getStatus());

        // 2. 违禁商品举报（处理中）
        Report report2 = new Report();
        report2.setReporter(user2);
        report2.setTargetType("PRODUCT");
        report2.setTargetId(product3.getId());
        report2.setReportType("ILLEGAL_ITEM");
        report2.setContent("该相机可能是赃物，建议核实来源");
        report2.setEvidenceUrl("http://example.com/evidence2.jpg");
        report2.setStatus("PROCESSING");
        System.out.println("\n创建违禁商品举报:");
        System.out.println("举报人: " + report2.getReporter().getUsername());
        System.out.println("目标类型: " + report2.getTargetType());
        System.out.println("目标ID: " + report2.getTargetId());
        System.out.println("举报类型: " + report2.getReportType());
        System.out.println("举报内容: " + report2.getContent());

        System.out.println("状态: " + report2.getStatus());

        // 3. 侵权内容举报（已完成）
        Report report3 = new Report();
        report3.setReporter(admin);
        report3.setTargetType("COMMENT");
        report3.setTargetId(comment5.getId());
        report3.setReportType("COPYRIGHT_INFRINGEMENT");
        report3.setContent("评论内容涉嫌诽谤，违反平台规则");
        report3.setEvidenceUrl("http://example.com/evidence3.jpg");
        report3.setStatus("COMPLETED");
        System.out.println("\n创建侵权内容举报:");
        System.out.println("举报人: " + report3.getReporter().getUsername());
        System.out.println("目标类型: " + report3.getTargetType());
        System.out.println("目标ID: " + report3.getTargetId());
        System.out.println("举报类型: " + report3.getReportType());
        System.out.println("举报内容: " + report3.getContent());

        System.out.println("状态: " + report3.getStatus());

        // 4. 垃圾信息举报（已驳回）
        Report report4 = new Report();
        report4.setReporter(user3);
        report4.setTargetType("COMMENT");
        report4.setTargetId(comment4.getId());
        report4.setReportType("SPAM");
        report4.setContent("评论内容涉嫌广告，包含联系方式");
        report4.setEvidenceUrl("http://example.com/evidence4.jpg");
        report4.setStatus("REJECTED");
        System.out.println("\n创建垃圾信息举报:");
        System.out.println("举报人: " + report4.getReporter().getUsername());
        System.out.println("目标类型: " + report4.getTargetType());
        System.out.println("目标ID: " + report4.getTargetId());
        System.out.println("举报类型: " + report4.getReportType());
        System.out.println("举报内容: " + report4.getContent());

        System.out.println("状态: " + report4.getStatus());

        // 保存举报数据
        reportRepository.saveAll(Arrays.asList(report1, report2, report3, report4));
        System.out.println("\n举报数据创建完成，当前举报数量: " + reportRepository.count());

        // 打印数据统计信息
        System.out.println("\n=== 测试数据初始化完成！数据统计：===");
        System.out.println("----------------------------------------");
        System.out.println("用户数量: " + userRepository.count());
        System.out.println("商品数量: " + productRepository.count());
        System.out.println("评论数量: " + commentRepository.count());
        System.out.println("举报数量: " + reportRepository.count());
        System.out.println("----------------------------------------");
        System.out.println("=== 数据初始化结束 ===\n");
    }
}